from datetime import datetime, date, time, timedelta


def get_offset_date(days=-1):
    """
    获取指定天数的偏移日期
    :param days:
    :return: 格式化后的字符串
    """
    today = date.today()
    offset = timedelta(days=days)
    return (today + offset).strftime('%Y-%m-%d 00:00:00')


def _parse_date(date_str):
    """
    解析日期字符串为 date 对象
    :param date_str:
    :return: date 对象
    """
    return datetime.strptime(date_str, "%Y-%m-%d").date()


def get_day_start_time(date_str):
    """
    获取指定日期的开始时间
    :param date_str:
    :return: 格式化后的字符串
    """
    specified_date = _parse_date(date_str)
    start_of_day = datetime.combine(specified_date, time.min)
    return start_of_day.strftime('%Y-%m-%d %H:%M:%S')


def get_day_end_time(date_str):
    """
    获取指定日期的结束时间
    :param date_str:
    :return: 格式化后的字符串
    """
    specified_date = _parse_date(date_str)
    end_of_day = datetime.combine(specified_date, time.max).replace(microsecond=0)
    return end_of_day.strftime('%Y-%m-%d %H:%M:%S')


def get_offset_date_end_time(days=-1):
    """
    获取指定天数的结束时间
    :param days:
    :return: 格式化后的字符串
    """
    today = date.today()
    offset = timedelta(days=days)
    return (today + offset).strftime('%Y-%m-%d 23:59:59')


def get_time_range(start_time, end_time):
    """
    获取时间段的起始和结束时间
    :param start_time:
    :param end_time:
    :return: 起始时间和结束时间字符串
    """
    return get_day_start_time(start_time), get_day_end_time(end_time)


def get_current_date():
    """
    获取当前日期
    :return: 格式化后的字符串
    """
    return date.today().strftime('%Y-%m-%d 00:00:00')


def get_yesterday_time_range():
    """
    获取昨天的起始时间和结束时间
    :return: 起始时间和结束时间字符串
    """
    yesterday = datetime.today() - timedelta(days=1)
    start_time = yesterday.strftime('%Y-%m-%d 00:00:00')
    end_time = yesterday.strftime('%Y-%m-%d 23:59:59')
    return start_time, end_time


def add_minutes_to_time(time_point=None, minutes=0):
    """
    Adds specified number of minutes to the given time point or current time if not provided.

    :param time_point: The time point to add minutes to. If None, uses current time.
    :param minutes: Number of minutes to add.
    :return: A new datetime object with the added minutes.
    """

    if time_point is None:
        time_point = datetime.now()
    return time_point + timedelta(minutes=minutes)


def get_minutes_to_time(hour=7, minutes=0, offset=5):
    """

    :param hour: 指定时间的小时数
    :param minutes: 指定时间的分钟数
    :param offset: 指定的偏移量
    :return:
    """
    time_point = datetime(2023, 4, 1, hour, minutes)  # April 1, 2023, 12:00
    new_specific_time = time_point + timedelta(minutes=offset)
    return new_specific_time.hour, new_specific_time.minute


if __name__ == '__main__':
    print(get_offset_date(-1))
    print(get_day_start_time('2025-04-06'), get_day_end_time('2025-04-09'))
    specific_time = datetime(2023, 4, 1, 7, 0)
    print(add_minutes_to_time(specific_time, 5).hour, add_minutes_to_time(specific_time, 5).minute)
    h, m = get_minutes_to_time(hour=7, minutes=5, offset=10)
    print(h, m)
    h, m = get_minutes_to_time(h, m)
    print(h, m)
    h, m = get_minutes_to_time(h, m)
    print(h, m)
